// pages/detail/detail.js
import req from '../../utils/req'
import utils from '../../utils/utils'

let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiper: {
      indicatorDots: 'true',
      indicatorColor: '#fff',
      indicatorActiveColor: '#87C6FF',
      autoplay: 'true',
      interval: '3000',
      circular: 'true',
    },
    collection: '',
    follow: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    that.setData({
      globalUid: app.globalData.userInfo.uid
    })
    that.getHeight()
    that.getStrategy(options.sid)
  },

  getStrategy(sid) {
    req.get('加载中', '/moment/getStrategy', {
      sid
    }, (res) => {
      const data = res.data.list[0]
      const images = []
      let taps = data['taps'].split(',')
      data['images'].forEach(e2 => {
        images.push(e2.url)
      })
      data.images = images
      data.taps = taps
      data['createTime'] = utils.timeFormat(data['createTime'])
      this.setData(data)
      this.selectCollection()
      this.getfollowState()
      this.addHistory()
    })
  },

  follow() {
    let follow = this.data.follow
    let id = utils.randomId()
    let data = {}
    if (follow !== '') {
      data = {
        fid: follow
      }
      req.post('', '/user/cancelFollow', data, (res) => {
        if (res.status === 200) {
          utils.toast('success', res.msg)
          this.setData({
            follow: ''
          })
        }
      })
    } else {
      data = {
        fid: id,
        followId: this.data.uid,
        uid: app.globalData.userInfo.uid
      }
      req.post('', '/user/createFollow', data, (res) => {
        if (res.status === 200) {
          utils.toast('success', res.msg)
          this.setData({
            follow: id
          })
        }
      })
    }
  },

  getfollowState() {
    const data = {
      followId: this.data.uid,
      uid: app.globalData.userInfo.uid
    }
    req.get('加载中', '/user/selectFollow', data, (res) => {
      if (res.data.total === 1) {
        this.setData({
          follow: res.data.list[0].fid
        })
      }
    })
  },
  collection() {
    const collection = this.data.collection
    const id = utils.randomId()
    let data = {}
    if (collection !== '') {
      data = {
        id: collection
      }
      req.post('', '/moment/cancelCollection', data, (res) => {
        if (res.status === 200) {
          utils.toast('success', res.msg)
          this.setData({
            collection: ''
          })
        }
      })
    } else {
      data = {
        id,
        uid: app.globalData.userInfo.uid,
        sid: this.data.sid
      }
      req.post('', '/moment/createCollection', data, (res) => {
        if (res.status === 200) {
          utils.toast('success', res.msg)
          this.setData({
            collection: id
          })
        }
      })
    }
    this.setData({
      collection: !this.data.collection
    })
  },
  selectCollection() {
    const data = {
      sid: this.data.sid,
      uid: app.globalData.userInfo.uid
    }
    req.get('加载中', '/moment/selectCollection', data, (res) => {
      if (res.data.total === 1) {
        this.setData({
          collection: res.data.list[0].id
        })
      }
    })
  },

  getHeight() {
    let that = this;
    // 获取系统信息
    wx.getSystemInfo({
      success: function (res) {
        // 获取可使用窗口宽度
        let clientHeight = res.windowHeight;
        // 获取可使用窗口高度
        let clientWidth = res.windowWidth;
        // 算出比例
        let ratio = 750 / clientWidth;
        // 算出高度(单位rpx)
        let height = clientHeight * ratio;
        // 设置高度
        that.setData({
          height: `${height-120}rpx`
        });
      }
    });
  },

  addHistory() {
    req.post('', '/user/createHistory', {
      uid: app.globalData.userInfo.uid,
      sid: this.data.sid,
    }, (res) => {
      return
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    try {
      let pages = getCurrentPages(); //页面栈
      let beforePage = pages[pages.length - 2];
      let aheadPage = pages[pages.length - 3];
      if (beforePage.route === "pages/collection/collection" || beforePage.route === "pages/history/history") {
        beforePage.search("search")
      }
      if (aheadPage.route === "pages/myfollow/myfollow") {
        aheadPage.search("search")
      }
    } catch (error) {}
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})